package com.valenx.distributedmulticamerasystem;

import com.valenx.distributedmulticamerasystem.camera.constant.RemoteCommand;
import com.valenx.distributedmulticamerasystem.utils.CommonEventUtil;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.common.BufferInfo;
import ohos.rpc.*;

import java.nio.ByteBuffer;

//远程服务
public class RemoteServiceAbility extends Ability {
    //日志相关
    static final String TAG = RemoteServiceAbility.class.getSimpleName();
    static final HiLogLabel LOG_LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00104, TAG);

    private CameraRemote cameraRemote = new CameraRemote();//相机代理对象

//===================================================onStart===================================================
//=============================================================================================================
    @Override
    public void onStart(Intent intent) {
        HiLog.error(LOG_LABEL, "RemoteServiceAbility::onStart");
        super.onStart(intent);
    }
//=============================================================================================================

    //连接时返回相机代理对象
    @Override
    public IRemoteObject onConnect(Intent intent) {
        super.onConnect(intent);
        return cameraRemote.asObject();
    }

//**************************************************相机代理类***************************************************

    //相机代理类
    public static class CameraRemote extends RemoteObject implements IRemoteBroker {
        private static final int ERR_OK = 0;

        private CameraRemote() {
            super("CameraRemote");
        }

        @Override
        public IRemoteObject asObject() {
            return this;
        }

        //接收到远程请求
        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
            HiLog.info(LOG_LABEL, "接收到远程请求......");
            reply.writeInt(ERR_OK);

            RemoteCommand remoteCommand = RemoteCommand.getByCode(code);

            switch (remoteCommand) {
                //远程拍摄
                case REMOTE_ACTION:
                    CommonEventUtil.publishRemoteActionEvent();
                    break;
                //改变远程相机模式
                case CHANGE_CAMERA_MODE:
                    int cameraModeId = data.readInt();
                    CommonEventUtil.publishChangeCameraModeEvent(cameraModeId);
                    break;
                //远程切换相机朝向
                case CHANGE_CAMERA_TYPE:
                    CommonEventUtil.publishChangeCameraTypeEvent();
                    break;
                //从机开始推流
                case START_PUSHFLOW:
                    CommonEventUtil.publishPushFlowEvent();
                    break;
                //从机停止推流
                case STOP_PUSHFLOW:
                    CommonEventUtil.publishStopPushFlowEvent();
                    break;
                //从机发送已编码数据给主机
                case SEND_ENCODED_DATA:
                    byte[] buffers = data.readByteArray();
                    String fromDeviceId = data.readString();
                    CommonEventUtil.publishSendEncodedData(buffers,fromDeviceId);
                    break;
            }

            return true;
        }
    }
//************************************************其他生命周期回调*************************************************

    @Override
    public void onBackground() {
        super.onBackground();
        HiLog.info(LOG_LABEL, "RemoteServiceAbility::onBackground");
    }

    @Override
    public void onStop() {
        super.onStop();
        HiLog.info(LOG_LABEL, "RemoteServiceAbility::onStop");
    }

    @Override
    public void onCommand(Intent intent, boolean restart, int startId) {
    }

    @Override
    public void onDisconnect(Intent intent) {
    }
}